package net.asfun.jvalog.common;

import static net.asfun.jvalog.common.log.logger;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

import com.google.appengine.api.memcache.stdimpl.GCacheFactory;

@SuppressWarnings("unchecked")
public class GMCacher {

	protected static Cache gmcache;
	
	static {
		Map props = new HashMap();
		props.put(GCacheFactory.EXPIRATION_DELTA, 36000);
		try {
			CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
			gmcache = cacheFactory.createCache(props);
		} catch (CacheException e) {
			logger.log(Level.WARNING, e.getMessage(), e);
		}
	}
	
	public static Object get(String key) {
		return gmcache.get(key);
	}
	
	public static void put(String key, Object value) {
		gmcache.put(key, value);
	}
	
	public static void remove(String key) {
		gmcache.remove(key);
	}
	
	public static void clear() {
		gmcache.clear();
	}
	
	public static String getStat() {
		return gmcache.getCacheStatistics().toString();
	}
}
